package com.corechina.apex_osswords.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

@Component
public class WxTemplateSend {

    public static void SendMessage() {
    }

    public String SendMessage(String tempId, String openid, String token, Map<String, Object> data, String toUrl, Integer request_id) {

        WxTemplateStorage wxTemplateStorage = new WxTemplateStorage();
        wxTemplateStorage.setTouser(openid);
        wxTemplateStorage.setTemplate_id(tempId);
        wxTemplateStorage.setPage(toUrl + request_id);
        wxTemplateStorage.setMiniprogram_state("formal");
        wxTemplateStorage.setLang("zh_CN");
        wxTemplateStorage.setData(data);

        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + token;
        return post(JSONObject.parseObject(JSON.toJSONString(wxTemplateStorage, true)), url);
    }

    public static String post(JSONObject json, String url) {
        String result = "";
        HttpPost post = new HttpPost(url);
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();

            post.setHeader("Content-Type", "application/json;charset=utf-8");
            post.addHeader("Authorization", "Basic YWRtaW46");
            StringEntity postingString = new StringEntity(json.toString(), "utf-8");
            post.setEntity(postingString);
            HttpResponse response = httpClient.execute(post);

            InputStream in = response.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                strber.append(line + '\n');
            }
            br.close();
            in.close();
            result = strber.toString();
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                result = "服务器异常";
            }
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        } finally {
            post.abort();
        }
        return result;
    }

    @Data
    public class WxTemplateStorage {
        @ApiModelProperty(value = "指定用户")
        private String touser;

        @ApiModelProperty(value = "模版id")
        private String template_id;

        @ApiModelProperty(value = "page")
        private String page;

        @ApiModelProperty(value = "语言")
        private String lang;

        @ApiModelProperty(value = "跳转小程序类型")
        private String miniprogram_state;

        @ApiModelProperty(value = "数据")
        private Map<String, Object> data;
    }
}
